package com.dkd.system.service.impl;

import java.util.Date;
import java.util.List;

import com.dkd.common.core.domain.AjaxResult;
import com.dkd.system.domain.TCodeRule;
import com.dkd.system.mapper.TCodeRuleMapper;
import com.dkd.system.utils.CodeGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dkd.system.mapper.TWarrantyMapper;
import com.dkd.system.domain.TWarranty;
import com.dkd.system.service.ITWarrantyService;

/**
 * listService业务层处理
 * 
 * @author chuyongshuo
 * @date 2025-08-07
 */
@Service
public class TWarrantyServiceImpl implements ITWarrantyService 
{
    @Autowired
    private TWarrantyMapper tWarrantyMapper;

    @Autowired
    private TCodeRuleMapper codeRuleMapper;

    /**
     * 查询list
     * 
     * @param id list主键
     * @return list
     */
    @Override
    public TWarranty selectTWarrantyById(Integer id)
    {
        return tWarrantyMapper.selectTWarrantyById(id);
    }

    /**
     * 查询list列表
     * 
     * @param tWarranty list
     * @return list
     */
    @Override
    public List<TWarranty> selectTWarrantyList(TWarranty tWarranty)
    {
        return tWarrantyMapper.selectTWarrantyList(tWarranty);
    }

    /**
     * 新增list
     * 
     * @param tWarranty list
     * @return 结果
     */
    @Override
    public int insertTWarranty(TWarranty tWarranty)
    {
        return tWarrantyMapper.insertTWarranty(tWarranty);
    }

    /**
     * 修改list
     * 
     * @param tWarranty list
     * @return 结果
     */
    @Override
    public int updateTWarranty(TWarranty tWarranty)
    {
        return tWarrantyMapper.updateTWarranty(tWarranty);
    }

    /**
     * 批量删除list
     * 
     * @param ids 需要删除的list主键
     * @return 结果
     */
    @Override
    public int deleteTWarrantyByIds(Integer[] ids)
    {
        return tWarrantyMapper.deleteTWarrantyByIds(ids);
    }

    /**
     * 删除list信息
     * 
     * @param id list主键
     * @return 结果
     */
    @Override
    public int deleteTWarrantyById(Integer id)
    {
        return tWarrantyMapper.deleteTWarrantyById(id);
    }

    /**
     * 获取证书编码
     *
     * @return 结果
     */
    @Override
    public AjaxResult getCode() {
        //首先查询数据库 获取编码规则
       TCodeRule codeRule = codeRuleMapper.selectTCodeRuleOneByStatus();
        //调取工具类传递编码参数
        //重置类型
        Integer resetType = codeRule.getResetType();
        //前缀
        String prefix = codeRule.getPrefix();
        //时间
        String dateFormat = codeRule.getDateFormat();
        //长度
        Integer seqLength = codeRule.getSeqLength();
        //获取当前值
        Integer currentValue = codeRule.getCurrentValue();
        //获取下一个数值
        int nextSeq = CodeGenerator.getNextSequence(currentValue, seqLength, resetType, new Date());
        String code = CodeGenerator.generateCode(prefix, dateFormat, seqLength, nextSeq);
        //获取当下个数值后，修改数据库存储字段
        codeRule.setCurrentValue(nextSeq);
        codeRule.setUpdateAt(new Date());
        int count =codeRuleMapper.updateTCodeRule(codeRule);
        //返回编码
        if (count==1){
            return AjaxResult.success(code);
        }else {
            return AjaxResult.error("数据库修改失败，获取编码未成功");
        }
    }

    @Override
    public void updateBookJson(String id, String jsonContent,String updatedBy) {
         tWarrantyMapper.updateBookJson(id,jsonContent,updatedBy);
    }

    @Override
    public TWarranty selectTWarrantyJsonById(Integer id) {
        return tWarrantyMapper.selectTWarrantyJsonById(id);
    }
}
